package com.crm.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.domain.Contract;
import com.crm.dto.CluesStatisticsListDto;
import com.crm.dto.CommonStatisticsPageQueryDto;
import com.crm.dto.ContractStatisticsPageQueryDto;
import com.crm.dto.Vo.*;

import com.crm.mapper.StatsMapper;
import com.crm.service.StatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class StatsServiceImpl extends ServiceImpl<StatsMapper, Contract> implements StatsService {
    @Autowired
    private StatsMapper statsMapper;

    @Override
    public List<SubjectStatisticsVo> getSubjectStatistics(LocalDateTime beginDateTime, LocalDateTime endDateTime) {
        return statsMapper.getSubjectStatistics(beginDateTime, endDateTime);
    }

    @Override
    public CommonPageQueryVo<Contract> contractPageQuery(ContractStatisticsPageQueryDto dto) {
        //mybatis分页插件分页查询
        Page<Contract> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        statsMapper.selectAll(page, dto);
        return new CommonPageQueryVo<>(page.getTotal(), page.getRecords());
    }

    @Override
    public ReportStatisticsVo contractStatistics(LocalDateTime beginDateTime, LocalDateTime endDateTime, LocalDate beginCreateTime, LocalDate endCreateTime) {
        List<String> dateList = new ArrayList<>();
        List<Integer> totalCustomerList = new ArrayList<>();
        List<Integer> newCustomerList = new ArrayList<>();
        // 查询开始日期之前的客户总数
        int beginDataTotalCustomers = statsMapper.getBeginDataTotalCustomers(beginDateTime);
        // 查询每日新增客户列表
        List<DetailDataVO> detailDataVOList = statsMapper.getDetailData(beginDateTime, endDateTime, beginCreateTime, endCreateTime);
        detailDataVOList.forEach(DetailDataVO -> {
            dateList.add(DetailDataVO.getDate());
            if (DetailDataVO.getNewCustomers() == 0) {
                newCustomerList.add(0);
                return;
            }
            newCustomerList.add(DetailDataVO.getNewCustomers());
        });
        // 添加每日客户总量
        for (Integer newCustomer : newCustomerList) {
            // 当日客户总数 = 当日之前的客户总数(开始日期之前的客户总数) + 当日新增客户
            if (newCustomer == null) {
                newCustomerList.add(0);
                continue;
            }
            totalCustomerList.add(beginDataTotalCustomers + newCustomer);
            // 更新开始日期之前的用户总量
            beginDataTotalCustomers += newCustomer;
        }
        // 每日新增客户数
        ReportNumVo newCustomerNumVo = new ReportNumVo();
        newCustomerNumVo.setName("新增客户数");
        newCustomerNumVo.setData(newCustomerList);

        // 每日客户总数
        ReportNumVo totalCustomerNumVo = new ReportNumVo();
        totalCustomerNumVo.setName("客户总数");
        totalCustomerNumVo.setData(totalCustomerList);

        // 封装数据
        ReportStatisticsVo reportStatisticsVo = new ReportStatisticsVo();
        reportStatisticsVo.setXAxis(dateList);
        List<ReportNumVo> customerNumVos = new ArrayList<>();
        customerNumVos.add(newCustomerNumVo);
        customerNumVos.add(totalCustomerNumVo);
        reportStatisticsVo.setSeries(customerNumVos);
        return reportStatisticsVo;


}

    @Override
    public CommonPageQueryVo<OwnerShipStatisticsVo> deptPageQuery(CommonStatisticsPageQueryDto dto) {
    //mybatis分页插件分页查询
        Page<OwnerShipStatisticsVo> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        statsMapper.selectDeptPage(page, dto);
        return new CommonPageQueryVo<>(page.getTotal(), page.getRecords());



    }

    @Override
    public CommonPageQueryVo<CluesStatisticsVo> cluesPageQuery(CluesStatisticsListDto dto) {
        //mybatis分页插件分页查询
        Page<CluesStatisticsVo> cluePage = new Page<>(dto.getPageNum(), dto.getPageSize());
//        statsMapper.selectCluesPage(cluePage, dto);
        return new CommonPageQueryVo<>(cluePage.getTotal(), cluePage.getRecords());


    }


}




























